﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.ExtentionMethods;

namespace Happy.Web.Mvc.Newtonsoft
{
    /// <summary>
    /// 反序列化的后续处理器注册处。
    /// </summary>
    public static class DeserializePostProcesserRegistry
    {
        private static readonly Dictionary<Type, IDeserializePostProcesser> _Processers
            = new Dictionary<Type, IDeserializePostProcesser>();

        /// <summary>
        /// 注册后续处理器。
        /// </summary>
        public static void RegisterProcesser<T>(IDeserializePostProcesser processer)
        {
            processer.MustNotNull("processer");

            _Processers.Add(typeof(T), processer);
        }

        /// <summary>
        /// 返回后续处理器。
        /// </summary>
        public static IDeserializePostProcesser GetProcesser(Type resultType)
        {
            resultType.MustNotNull("resultType");

            if (_Processers.ContainsKey(resultType))
            {
                return _Processers[resultType];
            }

            return new EmptyDeserializePostProcesser();
        }

        private class EmptyDeserializePostProcesser : IDeserializePostProcesser
        {
            public object OnDeserialized(object result)
            {
                return result;
            }
        }
    }
}
